package com.xtaller.sdboot.utils.helper;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import lombok.var;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author : Taller
 * @date : 2020/1/13 10:35
 * @Description : JSON操作辅助类
 */
public class JsonHelper extends JSONObject {
    /**
     * 序列化配置
     */
    private static SerializeConfig serializeConfig = new SerializeConfig();
    static {
        serializeConfig.put(Date.class, new SimpleDateFormatSerializer(DateHelper.FORMAT_DATETIME_Y4MDHM));
    }

    /**
     * 将Java对象转换为Json String
     * @param object
     * @return
     */
    public static String stringify(Object object){
        return toJSONString(object, serializeConfig);
    }

    /***
     * 将JSON字符串转换为java对象
     * @param jsonStr
     * @return
     */
    public static Map toMap(String jsonStr){
        return parseObject(jsonStr);
    }

    /***
     * 将JSON字符串转换为java对象
     * @param jsonStr
     * @return
     */
    public static LinkedHashMap toLinkedHashMap(String jsonStr){
        if(StringUtils.isEmpty(jsonStr)){
            return null;
        }
        return toJavaObject(jsonStr, LinkedHashMap.class);
    }

    /***
     * 将JSON字符串转换为java对象
     * @param jsonStr
     * @param clazz
     * @return
     */
    public static <T> T toJavaObject(String jsonStr, Class<T> clazz){
        return JSONObject.parseObject(jsonStr, clazz);
    }

    /***
     * 将任意对象转换为java对象
     * @param object
     * @param clazz
     * @return
     */
    public static <T> T o2t(Object object, Class<T> clazz){
        return JSONObject.parseObject(JSONObject.toJSONString(object), clazz);
    }
    /***
     * 将任意对象转换为json对象
     * @param object
     * @return
     */
    public static JSONObject o2j(Object object){
        return JSONObject.parseObject(JSONObject.toJSONString(object), JSONObject.class);
    }

    public JsonHelper() {
    }
    public static JsonHelper create() {
        return new JsonHelper();
    }

    public static JsonHelper create(String key, Object val) {
        return create().put(key, val);
    }
    public static List<JSONObject> list(){
        List<JSONObject> list = new ArrayList<>();
        return list;
    }

    public static List<JSONObject> list(JSONObject... objects){
        var list = list();
        for(var obj:objects){
            list.add(obj);
        }
        return list;
    }

    @Override
    public JsonHelper put(String key, Object val) {
        super.put(key, val);
        return this;
    }
    public static String o2s(Object obj){
        return JsonHelper.toJSONString(obj);
    }

    public static List<JSONObject> o2l(Object obj) {
        return JsonHelper.parseArray(o2s(obj), JSONObject.class);
    }
    public static List<JSONObject> s2l(String obj) {
        return JsonHelper.parseArray(obj, JSONObject.class);
    }
    public static JSONObject s2j(String obj) {
        return JsonHelper.parseObject(obj);
    }

    public static <T> T o2m(Object object, Class<T> cls){
        return JsonHelper.parseObject(o2s(object), cls);
    }
    public static <T> T s2m(String object, Class<T> cls){
        return JsonHelper.parseObject(object, cls);
    }

    public static HashMap o2m(Object object){
        return JsonHelper.parseObject(o2s(object), HashMap.class);
    }
    public static HashMap o2m(String object){
        return JsonHelper.parseObject(object, HashMap.class);
    }
    public static HashMap s2m(String object){
        return JsonHelper.parseObject(object, HashMap.class);
    }
}
